#include "StatePreLevel.h"
#include "Hollywood.h"

StatePreLevel::StatePreLevel()
{
    levelName = "Unknown level";
    author = "Unknown author";
    description = "No description available";
    background.loadSprite("images/backgrounds/default_pre_level_bg.png");
    background.disableTransparentColour();
    text.loadFont("font/Freebooter.ttf",48);
    text.setColour(BLACK);
    text.setRelativity(true);
    next = 0;
    pushed = true;
}

StatePreLevel::StatePreLevel(CRstring levelName, CRstring author, CRstring description, CRstring bgFileName)
{
    this->levelName = levelName;
    this->author = author;
    this->description = description;
    if (bgFileName != "")
    {
        background.loadSprite(bgFileName);
    }
    else
    {
        background.loadSprite("images/backgrounds/default_pre_level_bg.png");
    }
    background.disableTransparentColour();
    text.loadFont("font/Freebooter.ttf",48);
    text.setColour(BLACK);
    text.setRelativity(true);
    EFFECTS->fadeIn(1000,BLACK);
    next = 0;
    pushed = true;
}

StatePreLevel::~StatePreLevel()
{
    //
}

void StatePreLevel::update()
{
    EFFECTS->update();
    if (next != 0 && EFFECTS->hasFinished())
    {
        if (next > 0)
        {
            setNextState(STATE_LEVEL);
        }
        else
        {
            setNextState(STATE_MAIN);
        }
    }
}

void StatePreLevel::render()
{
    background.render();
    text.setPosition(70,270);
    text.print(levelName + "\n");
    text.print("by " + author + "\n");
    text.print(description);
    EFFECTS->render();
}

void StatePreLevel::userInput()
{
    input->update();

#ifdef PLATFORM_PC
    if(input->isQuit())
    {
        nullifyState();
        return;
    }
#endif

    if (EFFECTS->hasFinished())
    {
        if (input->isA() || input->isStart())
        {
            next = 1;
            EFFECTS->fadeOut(1000,BLACK);
        }
        else if (input->isB())
        {
            next = -1;
            EFFECTS->fadeOut(1000,BLACK);
        }
    }
    input->resetKeys();
}
